﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using TT.Common;
using GT.ORM;
using TT.ORM;
using SfcorpDLL.biz;

namespace Sfcorp.form
{
    public partial class frmAgyBnsBillAddNew : BaseFormDev
    {
        bizAgyBns bzAgyBns;
        etyChargeDtl eyChrgDtl;
        bool hadOneSuccess = false;
        TT.Common.DataProcType status = DataProcType.NEW;

        public frmAgyBnsBillAddNew(bizAgyBns ibzAgyBns, etyChargeDtl ieyChrgDtl, TT.Common.DataProcType iStatus)
        {
            InitializeComponent();
            bzAgyBns = ibzAgyBns;
            eyChrgDtl = ieyChrgDtl;

            status = iStatus;
        }

        private void frmBillItem_Load(object sender, EventArgs e)
        {
            #region 下拉数据
            cboCustomer.DataSource = dataProcessor.CopyIList<SfcorpDLL.ety.etyCustomer>(global.ilCustomer);

            cboItem.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilBillCode);

            cboIo_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilBillIO);

            cboItem_Unit.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilBillUnit);

            cboItemUnitPer.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilBillUnit);

            IList<TT.ORM.etyCodeRef> ilPort = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilPort);
            cboPortId.DataSource = ilPort;

            IList<TT.ORM.etyCodeRef> ilIE = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCIE);
            cboIeId.DataSource = ilIE;

            IList<TT.ORM.etyCodeRef> ilCntrCopr = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCntrCorp);
            cboCntrCoprCod.DataSource = ilCntrCopr;

            IList<TT.ORM.etyCodeRef> ilFeeWay = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilFeeWay);
            cboFeeWayId.DataSource = ilFeeWay;

            IList<TT.ORM.etyCodeRef> ilWorkWay = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilWorkWay);
            cboWorkWayId.DataSource = ilWorkWay;

            IList<TT.ORM.etyCodeRef> ilCntrSize = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCntrSize);
            cboCntr_Siz_Cod.DataSource = ilCntrSize;

            IList<TT.ORM.etyCodeRef> ilCntrType = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCntrType);
            cboCntr_Typ_Cod.DataSource = ilCntrType;

            cboCargo_Cod.DataSource = dataProcessor.CopyIList<etyCodeRef>(global.ilCargo);

            IList<etyCodeRef> ilSpcMod = dataProcessor.CopyIList<etyCodeRef>(global.ilBnsSpcMod);
            ilSpcMod.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboSpcMod.DataSource = ilSpcMod;
            IList<etyCodeRef> ilForeignVvd = dataProcessor.CopyIList<etyCodeRef>(global.ilForeignVvd);
            ilForeignVvd.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboForeignVvd.DataSource = ilForeignVvd;
            #endregion

            ClearForm();

            if(status== DataProcType.EDIT)
                TT.Common.formProcess.SetFormEntity(eyChrgDtl, cGroupControl2);
            GetCboText();

            cboItem.Focus();
        }
        private void ClearForm()
        {
            cboCustomer.SelectedIndex = -1;
            cboPortId.SelectedIndex = -1;
            cboCntrCoprCod.SelectedIndex = -1;
            cboIeId.SelectedIndex = -1;
            cboFeeWayId.SelectedIndex = -1;
            cboWorkWayId.SelectedIndex = -1;
            cboCntr_Siz_Cod.SelectedIndex = -1;
            cboCntr_Typ_Cod.SelectedIndex = -1;
            chkWgtFlag.Checked = false;
            txtCntr.Text = "";
            cboCargo_Cod.SelectedIndex = -1;

            cboItem.SelectedIndex = -1;
            cboIo_Flag.SelectedIndex = 0;
            cboItem_Unit.SelectedIndex = 1;
            cboItemUnitPer.SelectedIndex = 1;
            txtAmount.Text = "";

            txtPrice.Focus();
            txtPrice.SelectAll();

            if (status == DataProcType.EDIT)
            {
                string sType=string.Empty;
                foreach (System.Windows.Forms.Control c in cGroupControl2.Controls)
                {
                    sType = c.GetType().ToString();
                    if (sType == "Dev.Control.MultiColumnComboBox" || sType=="Dev.Control.cTextEdit")
                    {
                        c.Enabled = false;
                    }
                }
                txtPrice.Enabled = true;
            }
        }

        private void GetCboText()
        {
            cboCustomer.GetCodeText();
            cboPortId.GetCodeText();
            cboCntrCoprCod.GetCodeText();
            cboIeId.GetCodeText();
            cboFeeWayId.GetCodeText();
            cboWorkWayId.GetCodeText();
            cboCntr_Siz_Cod.GetCodeText();
            cboCntr_Typ_Cod.GetCodeText();

            cboItem.GetCodeText();
            cboIo_Flag.GetCodeText();
            cboItem_Unit.GetCodeText();
            cboItemUnitPer.GetCodeText();

            cboSpcMod.GetCodeText();
            cboForeignVvd.GetCodeText();
        }

        //保存按钮
        private void btOK_Click(object sender, EventArgs e)
        {
            bool succ = false;
            etyChargeDtl eyChrgNew = new etyChargeDtl();
            eyChrgNew = eyChrgDtl;

            TT.Common.formProcess.GetFormEntity(eyChrgNew,panelControl1);
            TT.Common.formProcess.GetFormEntity(eyChrgNew, cGroupControl1);
            TT.Common.formProcess.GetFormEntity(eyChrgNew, cGroupControl2);
            eyChrgNew.PRICE = eyChrgNew.PRICE.TrimStart('¥');
            eyChrgNew.AMOUNT = eyChrgNew.AMOUNT.TrimStart('¥');
            eyChrgNew.CUSTOMER_NAME = cboCustomer.GetValue("CUSTOMER_NAM");
            eyChrgNew.CHRG_DESC1 = cboPortId.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC2 = cboCntrCoprCod.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC3 = cboIeId.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC4 = cboFeeWayId.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC5 = cboWorkWayId.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC7 = cboCntr_Siz_Cod.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC8 = cboCntr_Typ_Cod.GetValue("CODE_DESC");
            eyChrgNew.CHRG_DESC26 = cboCargo_Cod.GetValue("CODE_DESC");

            eyChrgNew.ITEM_DESC = cboItem.GetValue("CODE_DESC");
            eyChrgNew.IO_FLAG_DESC = cboIo_Flag.GetValue("CODE_DESC");
            eyChrgNew.UNIT_DESC = cboItem_Unit.GetValue("CODE_DESC");
            eyChrgNew.PER_UNIT_DESC = cboItemUnitPer.GetValue("CODE_DESC");
            eyChrgDtl = eyChrgNew;
            eyChrgDtl.LST_MOD = "bnsAddBill";
            

            CheckParam(ref eyChrgDtl);

            if (status == DataProcType.NEW)
            {
                eyChrgDtl.CHARGE_SEQ = "";
                succ = bzAgyBns.Billing_Add_ChargeDtl(eyChrgDtl);
            }
            else
                succ = bzAgyBns.Billing_Upd_ChargeDtl(eyChrgDtl);
            
            if (succ)
            {
                hadOneSuccess = true;
                if (status == DataProcType.NEW && TT.HelpInfo.MsgBox.ShowQuestionBox("保存成功，就否继续添加？"))
                    ClearForm();
                else
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
            else
                TT.HelpInfo.MsgBox.ShowSuccessFailBox(succ);
       
        }

        private bool CheckParam(ref etyChargeDtl eyBillItem)
        {
            if (string.IsNullOrWhiteSpace(eyBillItem.ITEM))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("收费代码不能为空");
                cboItem.Focus();
                return false;
            }

            if (string.IsNullOrWhiteSpace(eyBillItem.IO_FLAG))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("收支标记不能为空");
                cboIo_Flag.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(eyBillItem.QTY))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("数量不能为空");
                txtQty.Focus();
                return false;
            }
            decimal quantity;
            if (!decimal.TryParse(eyBillItem.QTY, out quantity))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("数量格式错误");
                txtQty.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(eyBillItem.UNIT))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("单位不能为空");
                cboItem_Unit.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(eyBillItem.QTY_PER))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("每数量不能为空");
                cboItemUnitPer.Focus();
                return false;
            }
            decimal quantityPer;
            if (!decimal.TryParse(eyBillItem.QTY_PER, out quantityPer))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("每数量格式错误");
                txtQtyPer.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(eyBillItem.UNIT_PER))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("每单位不能为空");
                cboItemUnitPer.Focus();
                return false;
            }
            decimal price;
            if (!decimal.TryParse(eyBillItem.PRICE, out price))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("单价格式错误'" + eyBillItem.PRICE+"'");
                txtPrice.Focus();
                return false;
            }
            decimal amount;
            if (!decimal.TryParse(eyBillItem.AMOUNT, out amount))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("金额格式错误");
                txtAmount.Focus();
                return false;
            }

            return true;
        }

        private void txtQty_TextChanged(object sender, EventArgs e)
        {
            ResetAmount();
        }

        private void txtQtyPer_TextChanged(object sender, EventArgs e)
        {
            ResetAmount();
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            ResetAmount();
        }
        private void ResetAmount()
        {
            decimal qty;
            decimal qtyPer;
            decimal price;
            if (!decimal.TryParse(txtQty.Text, out qty) || !decimal.TryParse(txtQtyPer.Text, out qtyPer) 
                //|| !decimal.TryParse(txtPrice.Text.TrimStart('¥'), out price))
                || !decimal.TryParse(txtPrice.EditValue.ToString().Trim(), out price))
                return;
            txtAmount.Text = (qty * qtyPer * price).ToString();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            if (hadOneSuccess)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void frmAgyBnsBillAddNew_Activated(object sender, EventArgs e)
        {
            if (status == DataProcType.EDIT)
            {
                txtPrice.Focus();
                txtPrice.SelectAll();
            }
            else
                cboItem.Focus();
        }
    }
}
